import pymysql
from sqlalchemy import create_engine
from urllib.parse import quote_plus
import os
from dataclasses import dataclass
from dotenv import load_dotenv

load_dotenv()
private = os.environ['private']

@dataclass
class MySQLConfig():
    host_public = '119.254.68.156'
    port_public = 13306
    host_private = '10.100.30.151'
    port_private = 3306
    user = os.environ['mysql_user']
    password = os.environ['mysql_password']


@dataclass
class MySQLTableConfig():
    database_xenergy = 'xenergy-devops'
    table_xenergy_power_station = 'ods_3306_xenergy_devops_power_station_d' # 所有电站的数据
    table_xenergy_criterion_order = 'ods_3306_xenergy_devops_devops_criterion_order_d' # 标定电站信息
    table_xenergy_install_info_d = 'ods_3306_xenergy_devops_devops_criterion_install_info_d' # 运维平台的辐射表的施工倾角方位角
    table_xenergy_install_info_fix = 'ods_3306_xenergy_devops_devops_criterion_install_info_fix' # 修正的辐照表的倾角方位角
    table_xenergy_inverter_year_month = 'ods_mq_inverter_power_d_{year_month}' # 逆变器的数据，按月分表了

    database_ai = 'ai_server'
    table_pv_module = 'pv_module' # 整理的组件信息
    table_ai_meteo = 'meteorological_data_qc'


    test_table= 'tmp' # 测试库
    biaoding_test = 'meteo_test' # 装在真实电站上的测试辐照表、温度的数据

    backup = 'backup' # 用来备份保底小时数的库

def get_mysql_conn(database, private=True):
    # 获取MySQL链接
    if private: # 内网
        return pymysql.connect(host=MySQLConfig.host_private,
                               port=MySQLConfig.port_private,
                               user=MySQLConfig.user,
                               password=MySQLConfig.password,
                               database=database)
    else: # 公网
        return pymysql.connect(host=MySQLConfig.host_public,
                               port=MySQLConfig.port_public,
                               user=MySQLConfig.user,
                               password=MySQLConfig.password,
                               database=database)

def get_mysql_engine(database, private=True, pool_size=200):
    # for pandas read table as dataframe
    if private:
        conn_str = f"mysql+pymysql://{MySQLConfig.user}:{quote_plus(MySQLConfig.password)}@{MySQLConfig.host_private}:{MySQLConfig.port_private}/{database}"
        return create_engine(conn_str, pool_size=pool_size)
    else:
        conn_str = f"mysql+pymysql://{MySQLConfig.user}:{quote_plus(MySQLConfig.password)}@{MySQLConfig.host_public}:{MySQLConfig.port_public}/{database}"
        return create_engine(conn_str, pool_size=pool_size)

